/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package visiodent;

import java.awt.Graphics;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JApplet;

/**
 *
 * @author PROG5
 */
public class assvapi extends JApplet {

    /**
     * Initialization method that will be called after the applet is loaded into
     * the browser.
     */
    private String strNomRessource, strNomLecteur, strCodeAccess, strDateConsultation;
    private String strPathGIEDLL, strPathSesam;
    private String strPathDLL;
    private String cartePSData;
    private String carteVitaleData;
    private boolean dllIsLoaded;
    
    @Override
    public void init() {
        
        // TODO start asynchronous download of heavy resources
        try
        {
            AccessController.doPrivileged(
            new PrivilegedExceptionAction<Boolean>()
            {
                @Override
                public Boolean run()
                {                 
                    strNomRessource = null;
                    strNomLecteur = null;
                    strCodeAccess = null;
                    strDateConsultation = null;

                    strPathDLL = null; 
                    strPathGIEDLL = null;
                    strPathSesam = null;
                    
                    cartePSData = null;
                    carteVitaleData = null;     
                    
                    dllIsLoaded = false;
                    
                    return true;
                }
            });
         } catch (PrivilegedActionException e)
            {
            }       
    }
    
    public void loadDLL()
    {
        try
        {
            AccessController.doPrivileged(
            new PrivilegedExceptionAction<Boolean>()
            {
                @Override
                public Boolean run()
                {   
                    dllIsLoaded = true;
                    try{
                        System.load(strPathDLL + "\\assvapi.dll"); 
                    } catch (Exception e)
                        {
                            dllIsLoaded = false;                        
                        }
                    
                    return true;
                }
            });
         } catch (PrivilegedActionException e)
            {
            }       
    }
    
    public void initApplet(String pathDLL, String pathGIEDLL, String pathSesam)
    {
        final String _pathDLL = pathDLL;        
        final String _pathGIEDLL = pathGIEDLL;        
        final String _pathSesam = pathSesam;
        
        try
        {
            AccessController.doPrivileged(
            new PrivilegedExceptionAction<Boolean>()
            {
                @Override
                public Boolean run()
                {
                    // To do code here
                    strPathDLL = _pathDLL;
                    strPathGIEDLL = _pathGIEDLL;
                    strPathSesam = _pathSesam;
                    loadDLL();
                    
                    return true;
                }
            });
         } catch (PrivilegedActionException e)
            {
            } 
    }
    
    public void setPathDLL(String pathDLL)
    {
        final String path = pathDLL;
        try
        {
            AccessController.doPrivileged(
            new PrivilegedExceptionAction<Boolean>()
            {
                @Override
                public Boolean run()
                {
                    // To do code here
                    strPathDLL = path;
                    return true;
                }
            });
         } catch (PrivilegedActionException e)
            {
            }   
    }
    
    public void setPathGIEDLL(String pathGIEDLL)
    {
        final String path = pathGIEDLL;
        try
        {
            AccessController.doPrivileged(
            new PrivilegedExceptionAction<Boolean>()
            {
                @Override
                public Boolean run()
                {
                    // To do code here
                    strPathGIEDLL = path;
                    return true;
                }
            });
         } catch (PrivilegedActionException e)
            {
            } 
    }
    
    public void setPathSesam(String pathSesam)
    {
        final String path = pathSesam;
        try
        {
            AccessController.doPrivileged(
            new PrivilegedExceptionAction<Boolean>()
            {
                @Override
                public Boolean run()
                {
                    // To do code here
                    strPathSesam = path;
                    return true;
                }
            });
         } catch (PrivilegedActionException e)
            {
            }         
    }
    
    public void setNomRessource(String nomRessource)
    {
        final String nom = nomRessource;
        try
        {
            AccessController.doPrivileged(
            new PrivilegedExceptionAction<Boolean>()
            {
                @Override
                public Boolean run()
                {
                    // To do code here
                    strNomRessource = nom;
                    return true;
                }
            });
         } catch (PrivilegedActionException e)
            {
            }         
    }
    
    public void setNomLecteur(String nomLecteur)
    {
        final String nom = nomLecteur;
        try
        {
            AccessController.doPrivileged(
            new PrivilegedExceptionAction<Boolean>()
            {
                @Override
                public Boolean run()
                {
                    // To do code here
                    strNomLecteur = nom;
                    return true;
                }
            });
         } catch (PrivilegedActionException e)
            {
            }         
    }
    
    public void setCodeAccess(String codeAccess)
    {
        final String code = codeAccess;
        try
        {
            AccessController.doPrivileged(
            new PrivilegedExceptionAction<Boolean>()
            {
                @Override
                public Boolean run()
                {
                    // To do code here
                    strCodeAccess = code;
                    return true;
                }
            });
         } catch (PrivilegedActionException e)
            {
            }         
    }
    
    public void setDateConsultation(String dateConsultation)
    {
        final String date = dateConsultation;
        try
        {
            AccessController.doPrivileged(
            new PrivilegedExceptionAction<Boolean>()
            {
                @Override
                public Boolean run()
                {
                    // To do code here
                    strDateConsultation = date;
                    return true;
                }
            });
         } catch (PrivilegedActionException e)
            {
            }         
    }
    
    public void TransmitString(String webString, String filePath)
    {
        final String str = webString; 
        final String pFile = filePath;        
        AccessController.doPrivileged(new PrivilegedAction()
            {
                @Override
                public Object run()
                {
                    if (str != null)
                    {
                        DataOutputStream dos = null;  
                        java.io.File myFile = new java.io.File(pFile);
                    
                        try {
                            dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(myFile),128));
                        } catch (FileNotFoundException ex) {
                            Logger.getLogger(assvapi.class.getName()).log(Level.SEVERE, null, ex);                        
                        }
                    
                        try {
                            dos.writeBytes(str);
                            dos.flush();
                            dos.close();
                        } catch (IOException ex) {
                            Logger.getLogger(assvapi.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                    return null;               
                }
  
            });
    }

    public void lireCartePS()
    {        
        try
        {
            AccessController.doPrivileged(
            new PrivilegedExceptionAction<Boolean>()
            {
                @Override
                public Boolean run()
                {
                    final assvapi aSSV = new assvapi();        
                    byte[] bytes = aSSV.ssvLireCartePS(strNomRessource, strNomLecteur, strCodeAccess, strPathGIEDLL + "\\ssvw32.dll", strPathSesam + "\\sesam.ini");                       
                    try {
                        cartePSData = new String(bytes, "UTF-8");                       
                    } catch (UnsupportedEncodingException ex) {
                        Logger.getLogger(assvapi.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    return true;
                }
            });
         } catch (PrivilegedActionException e)
            {
            }   
    }
    
    public String getPSData()
    {
       return cartePSData;
    }    
    
    public String getVitaleData()
    {
        return carteVitaleData;
    }
    
    public String getNomRessource()
    {
       return strNomRessource;
    }    
    
    public String getNomLecteur()
    {
        return strNomLecteur;
    }
    public String getCodeAccess()
    {
       return strCodeAccess;
    }    
    
    public String getDateConsultation()
    {
        return strDateConsultation;
    }    
 
    public void lireDroitsVitale()
    {
        try
        {
            AccessController.doPrivileged(
            new PrivilegedExceptionAction<Boolean>()
            {
                @Override
                public Boolean run()
                {
                    final assvapi aSSV = new assvapi();        
                    byte[] bytes = aSSV.ssvLireDroitsVitale(strNomRessource, strNomLecteur, strCodeAccess, strDateConsultation, strPathGIEDLL + "\\ssvw32.dll", strPathSesam + "\\sesam.ini");                         
                    try {
                        carteVitaleData = new String(bytes, "UTF-8");
                    } catch (UnsupportedEncodingException ex) {
                        Logger.getLogger(assvapi.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    return true;
                }
            });
         } catch (PrivilegedActionException e)
            {
            }        
     
    }
    /*
    @Override
    // Testing
    public void paint(final Graphics g)
    {         
        try
        {
            AccessController.doPrivileged(
            new PrivilegedExceptionAction<Boolean>()
            {
                @Override
                public Boolean run()
                {
                    lireCartePS();
                    String strPS = getPSData();
                    g.drawString("Lecteur Carte PS ...", 20, 20);
                    if (strPS.length() > 0)            
                    {            
                        g.drawString("OK", 150, 20);
                        g.drawString("Length: " + Integer.toString(strPS.length()), 20, 40);
                        g.drawString(strPS, 20, 60);            
                    }
                    else             
                        g.drawString("NONE!", 150, 20);


                    lireDroitsVitale();       
                    String strVitale = getVitaleData();
                    g.drawString("Lecteur Carte Vitale: ", 20, 80);
                    if (strVitale.length() > 0)            
                    {
                        g.drawString("OK", 150, 80);
                        g.drawString("Length: " + Integer.toString(strVitale.length()), 20, 100);
                        g.drawString(strVitale, 20, 120);            
                    }
                    else             
                        g.drawString("NONE!", 150, 80);
                    
                    return true;
                }
            });
         } catch (PrivilegedActionException e)
            {
            } 
    }
    */
    private native byte[] ssvLireCartePS(String NomRessourcePS, String NomRessourceLecteur, String CodePorteurPS, String pathDLL, String pathSesam);    
    private native byte[] ssvLireDroitsVitale(String NomRessourcePS, String NomRessourceLecteur, String CodePorteurPS, String DateConsultation, String pathDLL, String pathSesam);
    // TODO overwrite start(), stop() and destroy() methods
}
